package com.liangsui.mss.controller.system;

import com.liangsui.mss.aspectj.Log;
import com.liangsui.mss.aspectj.enums.BusinessType;
import com.liangsui.mss.constants.Constants;
import com.liangsui.mss.domain.Menu;
import com.liangsui.mss.dto.MenuDto;
import com.liangsui.mss.service.MenuService;
import com.liangsui.mss.utils.ShiroSecurityUtils;
import com.liangsui.mss.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * MenuController
 *
 * @author zhangjiawei
 * @email 1533167396@qq.com
 * @date 2022/12/5 23:26
 */
@RestController
@RequestMapping("system/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    /**
     * @功能描述:查询所有菜单及权限信息
     */
    @GetMapping("listAllMenus")
    public AjaxResult listAllMenus(MenuDto menuDto) {
        List<Menu> menuList = this.menuService.listAllMenus(menuDto);
        return AjaxResult.success(menuList);
    }

    /**
     * @功能描述:查询菜单的下拉树
     */
    @GetMapping("selectMenuTree")
    public AjaxResult selectMenuTree() {
        MenuDto menuDto = new MenuDto();
        menuDto.setStatus(Constants.STATUS_TRUE);
        return AjaxResult.success(this.menuService.listAllMenus(menuDto));
    }

    /**
     * @功能描述:根据ID查询菜单和权限信息
     */
    @GetMapping("getMenuById/{id}")
    public AjaxResult getMenuById(@PathVariable @NotNull(message = "参数不能为空") Integer id) {
        return AjaxResult.success(this.menuService.getMenuById(id));
    }

    /**
     * 根据角色ID查询当前角色ID拥有的权限菜单【只查子节点的】
     *
     * @return
     */
    @GetMapping("getMenuIdsByRoleId/{roleId}")
    public AjaxResult getMenuIdsByRoleId(@NotNull(message = "参数不能为空") @PathVariable Long roleId) {
        List<Long> ids = this.menuService.getMenuIdsByRoleId(roleId);
        return AjaxResult.success(ids);
    }

    /**
     * @功能描述:添加菜单和权限信息
     */
    @PostMapping("addMenu")
    @Log(title = "添加菜单和权限信息", businessType = BusinessType.INSERT)
    public AjaxResult addMenu(@Validated MenuDto menuDto) {
        try {
            // 设置创建人
            menuDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
            return AjaxResult.toAjax(this.menuService.addMenu(menuDto));
        } catch (Exception e) {
            // 返回给切面
            return AjaxResult.ex(e.getMessage());
        }

    }


    /**
     * @功能描述:修改菜单和权限信息
     */
    @PutMapping("updateMenu")
    @Log(title = "修改菜单和权限信息", businessType = BusinessType.UPDATE)
    public AjaxResult updateMenu(@Validated MenuDto menuDto) {
        try {
            // 设置修改者
            menuDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
            return AjaxResult.toAjax(this.menuService.updateMenu(menuDto));
        } catch (Exception e) {
            return AjaxResult.ex(e.getMessage());
        }
    }


    /**
     * @功能描述:删除菜单数据
     * @Description:逻辑需要判断该删除菜单是否有子菜单
     */
    @DeleteMapping("deleteMenu/{menuid}")
    @Log(title = "删除菜单数据", businessType = BusinessType.DELETE)
    public AjaxResult deleteMenu(@PathVariable Long menuid) {
        try {
            if (this.menuService.hasChilenByMenuID(menuid)) {
                // true表示有
                return AjaxResult.fail("当前删除的菜单有子节点，请先删除子节点");
            }
            // 没有
            return AjaxResult.toAjax(this.menuService.deleteMenuById(menuid));
        } catch (Exception e) {
            return AjaxResult.ex(e.getMessage());
        }
    }
}
